package src.Players;

import src.GameEngine;
import src.Main;

public class Bank
{
    private GameEngine ge;

    public Bank()
    {
        ge = Main.m.ge;
    }

    public boolean bankItem(Player p, int itemId, int fromSlot, int amount)
    {
        if(p == null || amount <= 0 || fromSlot < 0 || fromSlot > 28 || p.items[fromSlot] <= -1 || p.itemsN[fromSlot] <= 0)
        {
            return false;
        }
        PlayerItems pi = new PlayerItems();
        if(ge.item.noted(p.items[fromSlot] - 1) || ge.item.stackable(p.items[fromSlot] - 1))
        {
            int toBankSlot = -1;
            if(amount > p.itemsN[fromSlot])
            {
                amount = p.itemsN[fromSlot];
            }
            if(!ge.item.noted(p.items[fromSlot] - 1))
            {
                if(bankItemAmt(p, p.items[fromSlot] - 1) > 0)
                {
                    toBankSlot = bankItemSlot(p, p.items[fromSlot] - 1);
                }
            }
            else
            {
                if(bankItemAmt(p, p.items[fromSlot] - 2) > 0)
                {
                    toBankSlot = bankItemSlot(p, p.items[fromSlot] - 2);
                }
            }
            if(toBankSlot == -1 && freeBankSlots(p) > 0)
            {
                int bankSlot = getEmptyBankSlot(p);
                if(bankSlot != -1)
                {
                    toBankSlot = bankSlot;
                }
                else
                {
                    p.frame.sendMessage(p, "Not enough bank space!");
                    pi = null;
                    return false;
                }
                if(!ge.item.noted(p.items[fromSlot] - 1))
                {
                    p.bankItems[toBankSlot] = p.items[fromSlot];
                }
                else
                {
                    p.bankItems[toBankSlot] = p.items[fromSlot] - 1;
                }
                if((p.bankItemsN[toBankSlot] + amount) <= ge.item.maxItemAmount && (p.bankItemsN[toBankSlot] + amount) > 0)
                {
                    p.bankItemsN[toBankSlot] += amount;
                }
                else
                {
                    p.frame.sendMessage(p, "Not enough bank space!");
                    pi = null;
                    return false;
                }
                pi.deleteItem(p, (p.items[fromSlot] - 1), fromSlot, amount);
                p.frame.resetItems(p, 5064);
                p.frame.resetBank(p);
                pi = null;
                return true;
            }
            else if(toBankSlot != -1)
            {
                if((p.bankItemsN[toBankSlot] + amount) <= ge.item.maxItemAmount && (p.bankItemsN[toBankSlot] + amount) > 0)
                {
                    p.bankItemsN[toBankSlot] += amount;
                }
                else
                {
                        p.frame.sendMessage(p, "Not enough bank space!");
                        pi = null;
                        return false;
                }
                pi.deleteItem(p, (p.items[fromSlot] - 1), fromSlot, amount);
                p.frame.resetItems(p, 5064);
                p.frame.resetBank(p);
                pi = null;
                return true;
            }
            else
            {
                p.frame.sendMessage(p, "Not enough bank space!");
                pi = null;
                return false;
            }
        }
        else
        {
            int toBankSlot = -1;
            if(bankItemAmt(p, p.items[fromSlot] - 1) > 0)
            {
                toBankSlot = bankItemSlot(p, p.items[fromSlot] - 1);
            }
            if(toBankSlot == -1 && freeBankSlots(p) > 0)
            {
                int bankSlot = getEmptyBankSlot(p);
                if(bankSlot != -1)
                {
                    toBankSlot = bankSlot;
                }
                else
                {
                    p.frame.sendMessage(p, "Not enough bank space!");
                    pi = null;
                    return false;
                }
                while (amount > 0)
                {
                    if(pi.invItemCount(p, itemId) > 0)
                    {
                        p.bankItems[toBankSlot] = p.items[pi.getItemSlot(p, itemId)];
                        p.bankItemsN[toBankSlot]++;
                        pi.deleteItem(p, (p.items[pi.getItemSlot(p, itemId)] - 1), pi.getItemSlot(p, itemId), 1);
                        amount--;
                    }
                    else
                    {
                         amount = 0;
                    }
                }
                p.frame.resetItems(p, 5064);
                p.frame.resetBank(p);
                pi = null;
                return true;
            }
            else if(toBankSlot != -1)
            {
                while(amount > 0)
                {
                    if(pi.invItemCount(p, itemId) > 0)
                    {
                        p.bankItemsN[toBankSlot]++;
                        pi.deleteItem(p, (p.items[pi.getItemSlot(p, itemId)] - 1), pi.getItemSlot(p, itemId), 1);
                        amount--;
                    }
                    else
                        amount = 0;
                }
                p.frame.resetItems(p, 5064);
                p.frame.resetBank(p);
                pi = null;
                return true;
            }
            else
            {
                p.frame.sendMessage(p, "Not enough bank space!");
                pi = null;
                return false;
            }
        }
    }

    public void fromBank(Player p, int itemId, int fromSlot, int amount)
    {
        if(amount > 0 && p != null)
        {
            PlayerItems pi = new PlayerItems();
            if(p.bankItems[fromSlot] > 0)
            {
                if(!p.takeAsNote)
                {
                    if(ge.item.stackable(p.bankItems[fromSlot] - 1))
                    {
                        if(p.bankItemsN[fromSlot] > amount)
                        {
                            if(pi.addItem(p, (p.bankItems[fromSlot] - 1), amount))
                            {
                                p.bankItemsN[fromSlot] -= amount;
                                p.frame.resetBank(p);
                                p.frame.resetItems(p, 5064);
                            }
                        }
                        else
                        {
                            if(pi.addItem(p, (p.bankItems[fromSlot] - 1), p.bankItemsN[fromSlot]))
                            {
                                p.bankItems[fromSlot] = 0;
                                p.bankItemsN[fromSlot] = 0;
                                p.frame.resetBank(p);
                                p.frame.resetItems(p, 5064);
                            }
                        }
                    }
                    else
                    {
                        while (amount > 0)
                        {
                            if(p.bankItemsN[fromSlot] > 0)
                            {
                                if(pi.addItem(p, (p.bankItems[fromSlot] - 1), 1))
                                {
                                        p.bankItemsN[fromSlot] += -1;
                                        amount--;
                                }
                                else
                                {
                                    amount = 0;
                                }
                            }
                            else
                            {
                                amount = 0;
                            }
                        }
                        p.frame.resetBank(p);
                        p.frame.resetItems(p, 5064);
                    }
                }
                else if(p.takeAsNote && ge.item.noted(p.bankItems[fromSlot]))
                {
                    if(p.bankItemsN[fromSlot] > amount)
                    {
                        if(pi.addItem(p, p.bankItems[fromSlot], amount))
                        {
                            p.bankItemsN[fromSlot] -= amount;
                            p.frame.resetBank(p);
                            p.frame.resetItems(p, 5064);
                        }
                    }
                    else
                    {
                        if(pi.addItem(p, p.bankItems[fromSlot], p.bankItemsN[fromSlot]))
                        {
                            p.bankItems[fromSlot] = 0;
                            p.bankItemsN[fromSlot] = 0;
                            p.frame.resetBank(p);
                            p.frame.resetItems(p, 5064);
                        }
                    }
                }
                else
                    p.frame.sendMessage(p, "Item can't be drawn as note.");
            }
        }
    }

    public int bankItemAmt(Player p, int itemId)
    {
        int amt = 0;
        if(itemId < 0 || p == null)
        {
            return 0;
        }
        for(int i = 0; i < ge.item.maxBankSize; i++)
        {
            if(p.bankItems[i] == (itemId + 1))
            {
                amt += p.bankItemsN[i];
            }
        }
        return amt;
    }

    private int bankItemSlot(Player p, int itemId)
    {
        int amt = 0;
        if(p == null)
        {
            return -1;
        }
        for(int i = 0; i < ge.item.maxBankSize; i++)
        {
            if(p.bankItems[i] == (itemId + 1))
            {
                return i;
            }
        }
        return -1;
   }

    private int freeBankSlots(Player p)
    {
        int freeS = 0;
        if(p == null)
        {
            return -1;
        }
        for(int i = 0; i < ge.item.maxBankSize; i++)
        {
            if(p.bankItems[i] <= 0)
            {
                freeS++;
            }
        }
        return freeS;
    }

    private int getEmptyBankSlot(Player p)
    {
        if(p == null)
        {
            return -1;
        }
        for(int i = 0; i < ge.item.maxBankSize; i++)
        {
            if(p.bankItems[i] <= 0)
            {
                return i;
            }
        }
        return -1;
    }
}